package com.hdu.other.unionSet;

public class lc839 {
    class Solution {

        int[] father;
        int n;

        public int numSimilarGroups(String[] strs)
        {
            n = strs.length;
            father = new int[n];
            build();
            for (int i = 0; i < strs.length; i++)
            {
                for (int j = i + 1; j < strs.length; j++)
                {
                    String str1 = strs[i];
                    String str2 = strs[j];
                    int diff = 0;
                    for (int k = 0; k < str1.length(); k++)
                    {
                        if (str1.charAt(k) != str2.charAt(k))
                        {
                            diff++;
                        }
                    }
                    if (diff == 0 || diff == 2)
                    {
                        union(i, j);
                    }
                }
            }

            return n;
        }

        void build() {
            for (int i = 0; i < n; i++)
            {
                father[i] = i;
            }
        }


        int find(int x)
        {
            if (father[x] != x)
            {
                father[x] = find(father[x]);
            }
            return father[x];
        }

        void union(int x, int y)
        {
            if (find(x) != find(y))
            {
                father[find(x)] = find(y);
                n--;
            }
        }
    }
}
